home *** CD-ROM | disk | FTP | other *** search
/ EnigmA Amiga Run 1997 February / EnigmA AMIGA RUN 15 (1997)(G.R. Edizioni)(IT)[!][issue 1997-02][PLANET CD V].iso / progs / sviluppo / pike-0.4.0 / lib / include / string.pre.pike < prev   
Text File  |  1997-01-10  |  1KB  |  72 lines

  1. #define BEGIN 32
  2.  
  3. /*
  4.  * Implode an array of strings to an english 'list'
  5.  * ie. ({"foo","bar","gazonk"}) beomces "foo, bar and gazonk"
  6.  */
  7. string implode_nicely(string *foo, string|void and)
  8. {
  9.   if(!and) and="and";
  10.   switch(sizeof(foo))
  11.   {
  12.   case 0: return "";
  13.   case 1: return foo[0];
  14.   default: return foo[0..sizeof(foo)-2]*", "+" "+and+" "+foo[-1];
  15.   }
  16. }
  17.  
  18. string strmult(string str, int num)
  19. {
  20. #if 1
  21.   num*=strlen(str);
  22.   while(strlen(str) < num) str+=str;
  23.   return str[0..num-1];
  24. #endif
  25. #if 0
  26.   return sprintf("%~n",str,strlen(str)*num);
  27. #endif
  28. }
  29.  
  30. void create()
  31. {
  32.   add_constant("strmult",strmult);
  33.   add_constant("implode_nicely",implode_nicely);
  34.  
  35.   master()->add_precompiled_program("/precompiled/string_buffer", class {
  36.     string *buffer=allocate(BEGIN);
  37.     int ptr=0;
  38.     
  39.     static void fix()
  40.     {
  41.       string tmp=buffer*"";
  42.       buffer=allocate(strlen(tmp)/128+BEGIN);
  43.       buffer[0]=tmp;
  44.       ptr=1;
  45.     }
  46.     
  47.     string get_buffer()
  48.     {
  49.       if(ptr != 1) fix();
  50.       return buffer[0];
  51.     }
  52.     
  53.     void append(string s)
  54.     {
  55.       if(ptr==sizeof(buffer)) fix();
  56.       buffer[ptr++]=s;
  57.     }
  58.     
  59.     mixed cast(string to)
  60.     {
  61.       if(to=="string") return get_buffer();
  62.       return 0;
  63.     }
  64.  
  65.     void flush()
  66.     {
  67.       buffer=allocate(BEGIN);
  68.       ptr=0;
  69.     }
  70.   });
  71. }
  72.